package io.renren.modules.maintenance.controller;


import io.renren.common.utils.R;
import io.renren.modules.maintenance.entity.MDeviceDetail;
import io.renren.modules.maintenance.entity.MServiceRecord;
import io.renren.modules.maintenance.entity.MWorkSheet;
import io.renren.modules.maintenance.mapper.MServiceRecordMapper;
import io.renren.modules.maintenance.service.IMDeviceDetailService;
import io.renren.modules.maintenance.service.IMServiceRecordService;
import io.renren.modules.maintenance.service.IMWorkSheetService;
import io.renren.modules.maintenance.util.DaoStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author iweb
 * @since 2020-08-07
 */
@Api(tags = "工作单相关接口")
@RestController
@RequestMapping("/worksheet")
public class MWorkSheetController {
    @Autowired
    IMDeviceDetailService imDeviceDetailService;
    @Autowired
    IMServiceRecordService imServiceRecordService;
    @Autowired
    IMWorkSheetService imWorkSheetService;

    /**
     * @description: 根据订单id，返回工作单及设备信息
     * @return:
     * @author: 刘子弹
     * @time: 2020/8/11 8:59
     */

    @GetMapping("/detail/{orderId}")
    public R getWorkSheetDetail(@PathVariable("orderId") String orderId) {
        MWorkSheet mWorkSheet = imWorkSheetService.getMWorksheetByOrderId(orderId);



        return R.ok()

                .put("otherss", mWorkSheet.getOther());

    }

    @GetMapping("/serviceDetail")
    @ApiOperation("根据维保项目类别查找具体工作单的所有维保项目记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "worksheetId", value = "工作单id", defaultValue = "1"),
            @ApiImplicitParam(name = "serviceRecordCate", value = "机械维护", defaultValue = "机械维护"),
    })
    public R getMachDetail( int worksheetId, String serviceRecordCate) {
        ArrayList<MServiceRecord> serviceRecordsList = (ArrayList) imServiceRecordService.listServiceRecords(serviceRecordCate, worksheetId);


        //建立索引映射优化前端查询
        Map indexMap = new HashMap();
        for (int i = 0; i < serviceRecordsList.size(); i++) {
            indexMap.put(serviceRecordsList.get(i).getServiceId(), i);
        }

        return R.ok().put("ServiceRecords", serviceRecordsList)
                .put("indexMap", indexMap);
    }





    @PostMapping("/servicesDetail")
    @ApiOperation("工作单录入维保项目信息")
    public R addServiceRecord(@RequestBody MServiceRecord mServiceRecord) {
        return DaoStatus.rBack(imServiceRecordService.inputServiceRecord(mServiceRecord));


    }

}
